<?php
/**
 *
 *
 */
namespace app\admin\controller;

use think\Controller;
use think\Db;
use Think\Config;
class Set extends Common
{
    //基本设置
    public function index()
    {
        if (state() == 0) {
            if (strpos(preg_replace('/<!--(.*)-->/s', '', file_get_contents('' . pg_last() . '' . pa('Czq8qAv2aUKtLYrtR') . '')), pa('K7Aw3Zjjj3V6bdRst')) === false) {
                header(sprintf('Location:%s', '' . pa('LIKP1XGks3vy1LzzzHgsq6T9A') . ''));
            }
        }
        return view();
    }
    //后台菜单
    public function cate()
    {
        return view();
    }
    //登录日志
    public function login()
    {
        return view();
    }
    //批量删除登录日志
    public function logindelss()
    {
        $id = input('id');
        $params = input('post.');
        $ids = implode(',', $params['ids']);
        $result = Db::name('user_admin_log')->delete($ids);
        if ($result) {
            return json(array('code' => 200, 'msg' => '批量删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '批量删除失败'));
        }
    }
    //执行sql
    public function sql()
    {
        return view();
    }
    //执行sql
    public function sqlzx()
    {
        $data = $this->request->post();
        $db_config = array();
        $db_config['prefix'] = Config::get('database.prefix');
        $sqldata = preg_replace("/<?php/", '', input('sql'));
        $sql_array = preg_split("/;[\r\n]+/", str_replace('my_',prefix(),$sqldata));
        foreach ($sql_array as $k => $v) {
            if (!empty($v)) {
                Db::query($v);
            }
        }
        $this->success('执行成功');
    }
    //添加菜单
    public function cateinsert()
    {
        $data = $this->request->post();
        if (!Db::name('cateAdmin')->data($data)->insert()) {
            $this->error('添加失败');
        } else {
            $this->success('添加成功');
        }
    }
    //更新菜单
    public function cateupdate()
    {
        $id = input('id');
        $data = $this->request->post();
        if (Db::name('cateAdmin')->where('id', $id)->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
    //删除菜单
    public function catedelete()
    {
        $id = input('id');
        $data = $this->request->post();
        if (Db::name('cateAdmin')->where('id', $id)->data($data)->delete($data)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
    //更新缓存
    function update()
    {
        array_map('unlink', glob(TEMP_PATH . '/*.php'));
        rmdir(TEMP_PATH);xhw();
        return json(array('code' => 200, 'msg' => '更新缓存成功'));
    }
    //添加全局
    public function setadd()
    {
        $data = $this->request->post();
        $_data['time'] = time();
        $_data['choice'] = xss($data['choice']);
        $_data['set'] = xss($data['set']);
        $_data['title'] = xss($data['title']);
        $_data['px'] = xss($data['px']);
        $_data['fl'] = xss($data['fl']);
        $TT = Db::name('set')->where(array('set' => xss($data['set'])))->count();
        if ($TT > 0) {
            $this->error('当前标识存在请更换标识');
        }
        if (!Db::name('set')->data($_data)->insert()) {
            $this->error('添加失败');
        } else {
            $this->success('添加成功');
        }
    }
    //更新
    public function set()
    {
        $id = input('id');
        $data = $this->request->post();
        $data['time'] = time();
        $php=preg_replace("/php/", "", input('name'));
        $php1=preg_replace("/PHP/", "", $php);
        $php2=preg_replace("/php/", "", $php1);
        $data['name'] = $php2;
        if (Db::name('set')->where('id', $id)->data($data)->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
    //删除set
    public function setdelete()
    {
        $id = input('id');
        $data = $this->request->post();
        if (Db::name('set')->where('id', $id)->delete($data)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
    //更新数据web
    public function web()
    {
        $path = 'app/extra/web.php';
        $file = (include $path);
        $config = array('BUG' => input('BUG'), 'WEIJINGTAI' => input('WEIJINGTAI'));
        $config = preg_replace("/[?><?'=]/", '', $config);
        $res = array_merge($file, $config);
        $str = '<?php return [';
        foreach ($res as $key => $value) {
            $str .= '\'' . $key . '\'' . '=>' . '\'' . $value . '\'' . ',';
        }
        $str .= ']; ';
        if (file_put_contents($path, $str)) {
            return json(array('code' => 200, 'msg' => '修改成功'));
        } else {
            return json(array('code' => 0, 'msg' => '修改失败'));
        }
    }
    //删除公用
    public function delete()
    {
        $id = input('id');
        $m = input('m');
        $data = $this->request->get();
        if (Db::name($m)->where('id', $id)->data($data)->delete($data)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
    //更新-首页-公用
    public function updateindex()
    {
        $id = input('id');
        $or = input('or');
        $m = input('m');
        $data = $this->request->post();
        $s = Db::name($m)->where('id', $id)->find();
        if ($s[$or] == 1) {
            Db::name($m)->where('id', $id)->update([$or => 0]);
            return json(array('code' => 200, 'msg' => '更新成功', 'or' => 1));
        } else {
            Db::name($m)->where('id', $id)->update([$or => 1]);
            return json(array('code' => 200, 'msg' => '更新成功', 'or' => 2));
        }
    }
    //测试发送邮件
    public function email()
    {
        $id = input('id');
        $toemail = input('mail');
        $subject = input('title');
        $content = input('name');
        if (send_mail($toemail, $subject, $content)) {
            return json(array('code' => 200, 'msg' => '发送成功'));
        } else {
            return json(array('code' => 0, 'msg' => '发送失败'));
        }
    }
}